<template>
  <div class="company_add">
    <el-card class="box-card">
      <div class="title">
        <span>添加合作单位</span>
      </div>
      <div class="detail">
        <el-form
          :model="addData"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
        >
          <el-form-item label="公司名称" prop="company_name">
            <el-input v-model="addData.company_name" clearable></el-input>
          </el-form-item>
          <el-form-item label="负责人" prop="company_director">
            <el-input v-model="addData.company_director" clearable></el-input>
          </el-form-item>
          <el-form-item label="联系手机" prop="company_tel">
            <el-input v-model="addData.company_tel" clearable></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="company_email">
            <el-input v-model="addData.company_email" clearable></el-input>
          </el-form-item>
          <!-- <el-form-item label="状态" prop="status">
            <el-switch
              v-model="addData.status"
              active-text="开启"
              inactive-text="关闭"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </el-form-item> -->
        </el-form>
        <div class="btn">
          <el-button icon="el-icon-arrow-left" @click="back">返回</el-button>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
            style="margin-left: 50px"
            >提交<i class="el-icon-upload el-icon--right"></i
          ></el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Company_Add",
  inject: ["companyRefresh"],
  data() {
    return {
      addData: {
        company_name: "",
        company_director: "",
        company_tel: "",
        company_email: "",
      },
      rules: {
        company_name: [
          { required: true, message: "请输入合作单位名称", trigger: "blur" },
        ],
        company_director: [
          { required: true, message: "请输入负责人", trigger: "blur" },
        ],
        company_tel: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
        ],
        company_email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    back() {
      this.$store.commit("maskStatus_change", false);
      this.$router.go(-1);
      this.pageRefresh();
    },
    //刷新页面
    pageRefresh() {
      this.companyRefresh();
    },
    //提交添加合作单位信息
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios
            .post("/company/addCompany", this.addData, {
              "Content-Type": "application/json",
            })
            .then((res) => {
              if (res.data.code === "200") {
                this.$message({
                  message: res.data.msg,
                  type: "success",
                });
                this.back();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((err) => {
              console.error(err);
              this.$message.error("添加合作单位失败");
            });
        } else {
          this.$message({
            message: "请完整填写修改信息",
            type: "warning",
          });
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.box-card {
  position: absolute;
  left: 40%;
  top: 10%;
  width: 480px;
  height: 550px;
  z-index: 999;
}
.title {
  width: 90%;
  height: 5vh;
  padding: 0 0 0 30px;
  line-height: 3vh;
  text-align: left;
  font-size: 1.2rem;
  font-weight: 600;
  border-bottom: 1px dashed #c4c6c7;
}
.title span {
  display: inline-block;
  width: 80%;
  height: 55%;
  padding: 0px 20px;
  border-left: 5px solid #3b83cc;
}
.detail {
  margin: 50px auto;
  width: 80%;
}
.detail /deep/ .el-input {
  width: 85%;
}
.detail /deep/ .el-form-item__error {
  margin-left: 8%;
}
.detail /deep/ .el-form-item__label {
  margin-top: 10px;
}
.detail /deep/ .el-form-item__content {
  margin-top: 10px;
}
.btn {
  position: absolute;
  left: 50%;
  bottom: 5%;
  transform: translate(-50%, 0);
  margin-bottom: 0;
}
</style>